from selenium import webdriver
from lxml import etree
def BookInformation():
    url = "https://www.ptpress.com.cn/shopping/search?tag=search&orderStr=hot&level1=75424c57-6dd7-4d1f-b6b9-8e95773c0593"
    driver = webdriver.Chrome()
    driver.get(url)
    driver.implicitly_wait(10)
    html = driver.page_source
    tree = etree.HTML(html)
    # 图书信息在某些特定的标签中，比如<div class="book-item">，具体请根据实际网页结构调整
    book_items = tree.xpath("//li[@class='item']")  # 这里需要替换为实际的XPath表达式
    books_info = []
    for item in book_items:
        # 解析每个书籍项，根据实际网页结构调整XPath
        title = item.xpath(".//p[1]/a/text()")  # 书名
        price = item.xpath(".//p[2]/a/text()")  # 价格
        # 控制台打印
        zh = list(zip(title,price))
        print(zh)
        # 保存信息到列表
        books_info.append(f"{title},{price}\n")
        # 保存到本地文件
        with open("图书信息.txt", "w", encoding="utf-8") as file:
            file.writelines(books_info)
        print("图书信息已保存至本地文件。")

if __name__ == "__main__":
        BookInformation()
